﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Collections;
using SocketLib;
using System.IO;
using System.Threading;

namespace LogSvc
{
	//static class Program
	//{
	//    /// <summary>
	//    /// 应用程序的主入口点。
	//    /// </summary>
	//    [STAThread]
	//    static void Main()
	//    {
	//        Application.EnableVisualStyles();
	//        Application.SetCompatibleTextRenderingDefault(false);
	//        Application.Run(new Form1());
	//    }
	//}
	class Program
	{

		static void Main(string[] args)
		{

			runs c = new runs();
			c.start();
             string y = Convert.ToString(Console.ReadLine());
             if (y == "s")
             {
                 for (int i = 0; i < 999999; i++)
                 {
					 Thread.Sleep(10);
                     c.SendMsg(i.ToString());
                 }
             }
             Console.ReadKey();
           
		}

	}

	class runs
	{

		SocketLib.Server2 s = new SocketLib.Server2();

		public void start()
		{
			s.Start();
			//客户端连接上
			s.Accept_EventHandler += new SocketLib.Server2.Accept_Delegate(s_AcceptEventHandler);

			//接收到消息
			s.Callback_EventHandler += new SocketLib.Server2.CallbackDelegate(s_CallbackEventHandler);

			//关闭事件
			s.Closed_EventHandler += new Server2.Closed_Delegate(s_Closed_EventHandler);

		}

        public void SendMsg(string msg)
        {
            s.Send(msg);
        }

		//客户端关闭连接
		void s_Closed_EventHandler(object sender, Server2.Closed_EventArgs e)
		{
			Console.WriteLine(e.socket.RemoteEndPoint.ToString() + "断开连接");
		}

		void s_CallbackEventHandler(object sender, SocketLib.Server2.CallbackEventArgs e)
		{

			IPMPack c = new IPMPack();
			c.UnPacked(e.buffer);

			string str = System.Text.Encoding.Default.GetString(c.Data);
			Console.WriteLine(str);


		}

		void s_AcceptEventHandler(object sender, SocketLib.Server2.Accept_EventArgs e)
		{
			Console.WriteLine(e.ip);
             s.Send("OK....");
            
		}

	}


}
